home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / xshp15.zip / FILLCNVX.C < prev    next >
Text File  |  1991-12-01  |  8KB  |  173 lines

  1. /* Color-fills a convex polygon. All vertices are offset by (XOffset,
  2.    YOffset). "Convex" means that every horizontal line drawn through
  3.    the polygon at any point would cross exactly two active edges
  4.    (neither horizontal lines nor zero-length edges count as active
  5.    edges; both are acceptable anywhere in the polygon), and that the
  6.    right & left edges never cross. (It's OK for them to touch, though,
  7.    so long as the right edge never crosses over to the left of the
  8.    left edge.) Nonconvex polygons won't be drawn properly. Returns 1
  9.    for success, 0 if memory allocation failed */
  10.  
  11. #include <stdio.h>
  12. #include <math.h>
  13. #ifdef __TURBOC__
  14. #include <alloc.h>
  15. #else    /* MSC */
  16. #include <malloc.h>
  17. #endif
  18. #include "polygon.h"
  19.  
  20. /* Advances the index by one vertex forward through the vertex list,
  21.    wrapping at the end of the list */
  22. #define INDEX_FORWARD(Index) \
  23.    Index = (Index + 1) % VertexList->Length;
  24.  
  25. /* Advances the index by one vertex backward through the vertex list,
  26.    wrapping at the start of the list */
  27. #define INDEX_BACKWARD(Index) \
  28.    Index = (Index - 1 + VertexList->Length) % VertexList->Length;
  29.  
  30. /* Advances the index by one vertex either forward or backward through
  31.    the vertex list, wrapping at either end of the list */
  32. #define INDEX_MOVE(Index,Direction)                                  \
  33.    if (Direction > 0)                                                \
  34.       Index = (Index + 1) % VertexList->Length;                      \
  35.    else                                                              \
  36.       Index = (Index - 1 + VertexList->Length) % VertexList->Length;
  37.  
  38. extern void DrawHorizontalLineList(HLineList *, int);
  39. void ScanEdge(int, int, int, int, int, int, HLine **);
  40.  
  41. int FillConvexPolygon(PointListHeader * VertexList, int Color,
  42.       int XOffset, int YOffset)
  43. {
  44.    int i, MinIndexL, MaxIndex, MinIndexR, SkipFirst, Temp;
  45.    int MinPoint_Y, MaxPoint_Y, TopIsFlat, LeftEdgeDir;
  46.    int NextIndex, CurrentIndex, PreviousIndex;
  47.    int DeltaXN, DeltaYN, DeltaXP, DeltaYP;
  48.    HLineList WorkingHLineList;
  49.    HLine *EdgePointPtr;
  50.    Point *VertexPtr;
  51.  
  52.    /* Point to the vertex list */
  53.    VertexPtr = VertexList->PointPtr;
  54.  
  55.    /* Scan the list to find the top and bottom of the polygon */
  56.    if (VertexList->Length == 0)
  57.       return(1);  /* reject null polygons */
  58.    MaxPoint_Y = MinPoint_Y = VertexPtr[MinIndexL = MaxIndex = 0].Y;
  59.    for (i = 1; i < VertexList->Length; i++) {
  60.       if (VertexPtr[i].Y < MinPoint_Y)
  61.          MinPoint_Y = VertexPtr[MinIndexL = i].Y; /* new top */
  62.       else if (VertexPtr[i].Y > MaxPoint_Y)
  63.          MaxPoint_Y = VertexPtr[MaxIndex = i].Y; /* new bottom */
  64.    }
  65.    if (MinPoint_Y == MaxPoint_Y)
  66.       return(1);  /* polygon is 0-height; avoid infinite loop below */
  67.  
  68.    /* Scan in ascending order to find the last top-edge point */
  69.    MinIndexR = MinIndexL;
  70.    while (VertexPtr[MinIndexR].Y == MinPoint_Y)
  71.       INDEX_FORWARD(MinIndexR);
  72.    INDEX_BACKWARD(MinIndexR); /* back up to last top-edge point */
  73.  
  74.    /* Now scan in descending order to find the first top-edge point */
  75.    while (VertexPtr[MinIndexL].Y == MinPoint_Y)
  76.       INDEX_BACKWARD(MinIndexL);
  77.    INDEX_FORWARD(MinIndexL); /* back up to first top-edge point */
  78.  
  79.    /* Figure out which direction through the vertex list from the top
  80.       vertex is the left edge and which is the right */
  81.    LeftEdgeDir = -1; /* assume left edge runs down thru vertex list */
  82.    if ((TopIsFlat = (VertexPtr[MinIndexL].X !=
  83.          VertexPtr[MinIndexR].X) ? 1 : 0) == 1) {
  84.       /* If the top is flat, just see which of the ends is leftmost */
  85.       if (VertexPtr[MinIndexL].X > VertexPtr[MinIndexR].X) {
  86.          LeftEdgeDir = 1;  /* left edge runs up through vertex list */
  87.          Temp = MinIndexL;       /* swap the indices so MinIndexL   */
  88.          MinIndexL = MinIndexR;  /* points to the start of the left */
  89.          MinIndexR = Temp;       /* edge, similarly for MinIndexR   */
  90.       }
  91.    } else {
  92.       /* Point to the downward end of the first line of each of the
  93.          two edges down from the top */
  94.       NextIndex = MinIndexR;
  95.       INDEX_FORWARD(NextIndex);
  96.       PreviousIndex = MinIndexL;
  97.       INDEX_BACKWARD(PreviousIndex);
  98.       /* Calculate X and Y lengths from the top vertex to the end of
  99.          the first line down each edge; use those to compare slopes
  100.          and see which line is leftmost */
  101.       DeltaXN = VertexPtr[NextIndex].X - VertexPtr[MinIndexL].X;
  102.       DeltaYN = VertexPtr[NextIndex].Y - VertexPtr[MinIndexL].Y;
  103.       DeltaXP = VertexPtr[PreviousIndex].X - VertexPtr[MinIndexL].X;
  104.       DeltaYP = VertexPtr[PreviousIndex].Y - VertexPtr[MinIndexL].Y;
  105.       if (((long)DeltaXN * DeltaYP - (long)DeltaYN * DeltaXP) < 0L) {
  106.          LeftEdgeDir = 1;  /* left edge runs up through vertex list */
  107.          Temp = MinIndexL;       /* swap the indices so MinIndexL   */
  108.          MinIndexL = MinIndexR;  /* points to the start of the left */
  109.          MinIndexR = Temp;       /* edge, similarly for MinIndexR   */
  110.       }
  111.    }
  112.  
  113.    /* Set the # of scan lines in the polygon, skipping the bottom edge
  114.       and also skipping the top vertex if the top isn't flat because
  115.       in that case the top vertex has a right edge component, and set
  116.       the top scan line to draw, which is likewise the second line of
  117.       the polygon unless the top is flat */
  118.    if ((WorkingHLineList.Length =
  119.          MaxPoint_Y - MinPoint_Y - 1 + TopIsFlat) <= 0)
  120.       return(1);  /* there's nothing to draw, so we're done */
  121.    WorkingHLineList.YStart = YOffset + MinPoint_Y + 1 - TopIsFlat;
  122.  
  123.    /* Get memory in which to store the line list we generate */
  124.    if ((WorkingHLineList.HLinePtr =
  125.          (HLine *) (malloc(sizeof(HLine) *
  126.          WorkingHLineList.Length))) == NULL)
  127.       return(0);  /* couldn't get memory for the line list */
  128.  
  129.    /* Scan the left edge and store the boundary points in the list */
  130.    /* Initial pointer for storing scan converted left-edge coords */
  131.    EdgePointPtr = WorkingHLineList.HLinePtr;
  132.    /* Start from the top of the left edge */
  133.    PreviousIndex = CurrentIndex = MinIndexL;
  134.    /* Skip the first point of the first line unless the top is flat;
  135.       if the top isn't flat, the top vertex is exactly on a right
  136.       edge and isn't drawn */
  137.    SkipFirst = TopIsFlat ? 0 : 1;
  138.    /* Scan convert each line in the left edge from top to bottom */
  139.    do {
  140.       INDEX_MOVE(CurrentIndex,LeftEdgeDir);
  141.       ScanEdge(VertexPtr[PreviousIndex].X + XOffset,
  142.             VertexPtr[PreviousIndex].Y,
  143.             VertexPtr[CurrentIndex].X + XOffset,
  144.             VertexPtr[CurrentIndex].Y, 1, SkipFirst, &EdgePointPtr);
  145.       PreviousIndex = CurrentIndex;
  146.       SkipFirst = 0; /* scan convert the first point from now on */
  147.    } while (CurrentIndex != MaxIndex);
  148.  
  149.    /* Scan the right edge and store the boundary points in the list */
  150.    EdgePointPtr = WorkingHLineList.HLinePtr;
  151.    PreviousIndex = CurrentIndex = MinIndexR;
  152.    SkipFirst = TopIsFlat ? 0 : 1;
  153.    /* Scan convert the right edge, top to bottom. X coordinates are
  154.       adjusted 1 to the left, effectively causing scan conversion of
  155.       the nearest points to the left of but not exactly on the edge */
  156.    do {
  157.       INDEX_MOVE(CurrentIndex,-LeftEdgeDir);
  158.       ScanEdge(VertexPtr[PreviousIndex].X + XOffset - 1,
  159.             VertexPtr[PreviousIndex].Y,
  160.             VertexPtr[CurrentIndex].X + XOffset - 1,
  161.             VertexPtr[CurrentIndex].Y, 0, SkipFirst, &EdgePointPtr);
  162.       PreviousIndex = CurrentIndex;
  163.       SkipFirst = 0; /* scan convert the first point from now on */
  164.    } while (CurrentIndex != MaxIndex);
  165.  
  166.    /* Draw the line list representing the scan converted polygon */
  167.    DrawHorizontalLineList(&WorkingHLineList, Color);
  168.  
  169.    /* Release the line list's memory and we're successfully done */
  170.    free(WorkingHLineList.HLinePtr);
  171.    return(1);
  172. }
  173.